package services.dwzkdj;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import com.Const;
import com.PublicSystem;
import com.util.Util;
/**
 * 单位制卡登记Service
 * @author Administrator
 *
 */
public class DwzkServices {
	PublicSystem sys = PublicSystem.getInstance();
	/**
	 * 银行下拉框选项
	 * @param lx
	 * @return
	 * @throws Exception
	 */
	public List getYinhang(String sj) throws Exception {
		String sql = "select aaa103,aaa102 from v_ic_az11 where aaa100=?";
		List data = sys.query(sql, new Object[] {sj}, true);
		return data;
	}
	/**
	 * 下拉框选项
	 * @param lx
	 * @return
	 * @throws Exception
	 */
	public List getSelect(String lx) throws Exception {
		String sql = "select aaa103,aaa102 FROM aa10 where aaa100=?";
		List data = sys.query(sql, new Object[] {lx}, true);
		return data;
	}
	/**
	 * 查询单位登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public HashMap getUnitData(String id) throws Exception {
		String sql = "select * from ab01  where aab001= ? and aab620='0'";
		List data = sys.query(sql, new Object[] {id}, true);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	}
	
	/**
	 * 查询批量信息记录
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnit(Object[] obj,HashMap parms) throws Exception {
		String sql = "select c1,c2,c3,c4,c5,execmsg from zj_tmp where eventtype='AZ20' and eventid= ? " ;			
		/*调用统一表单方法并返回map*/
		return  sys.getGrid(sql, obj, parms, true);
	}
	/**
	 * 查询批量信息记录
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap getzkpc(Object[] obj,HashMap parms) throws Exception {
		String sql = "select * from v_az20 where aaz001 = ? and aaz411 ='10' " ;			
		/*调用统一表单方法并返回map*/
		return  sys.getGrid(sql, obj, parms, true);
	}
	/**
	 * 查询批量信息记录
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap gethkpc(Object[] obj,HashMap parms) throws Exception {
		String sql = "select * from v_az20 where aaz001 = ? and aaz411 like '%H%' " ;			
		/*调用统一表单方法并返回map*/
		return  sys.getGrid(sql, obj, parms, true);
	}
	/**
	 * 查询单位登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public HashMap getUnitData2(String id) throws Exception {
		String sql = "select AAC001,AAC003, AAC058,AAC002,AAC004,AAC005,AAC058 from AC01 where AAC001= ? ";
		List data = sys.query(sql, new Object[] {id}, true);
		String sql1 = "select aac100 from az02 where AAC001='" + id + "'" ;       
		String path = Const.server_path + "/Picture/";
		String name = id + "." + "JPG";
		String file = path + name;
		File files = new File(path);
		if(!files.exists()) {
			files.mkdirs();
		}	
		boolean flag = sys.SelResultBlob(sql1,file);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	}
	/**
	 * 查询单位登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public HashMap getgrUnitData(String id) throws Exception {
		String sql = "select aac004,aac058,aac005,aac003,aac002,aae005,aac006 from ac01  where AAC001= ? ";
		String sql1 = "select aac100 from az02 where AAC001='" + id + "'" ;       
		String path = Const.server_path + "/Picture/";
		String name = id + "." + "JPG";
		String file = path + name;
		File files = new File(path);
		if(!files.exists()) {
			files.mkdirs();
		}	
		
		boolean flag = sys.SelResultBlob(sql1, file);			
		List data = sys.query(sql, new Object[] {id}, true);	
		HashMap map = new HashMap();
		if(data.size() > 0) {
		    map = (HashMap)data.get(0);
		    String aac003 = map.get("aac003").toString();
		    if(null != aac003)
		    {
		    	String aaz742 = Util.getPingYin(aac003);
		    	map.put("aaz742", aaz742);
		    }		    
			String sql2 = "select b.aab001,b.aab004 from aca1 a, ab01 b where a.aab001=b.aab001 and b.aab620='0' and a.AAC001=?";
			List data2 = sys.query(sql2, new Object[] {id}, true);
			if(data2.size() > 0 ) { 
				HashMap obj = (HashMap)data2.get(0);
				map.put("aab001", obj.get("aab001"));
				map.put("aab004", obj.get("aab004"));
			}
		} else {
			return null;
		}
		return map;
	}
	/**
	 * 获取单位制卡批次
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String getMakeDWPc(HashMap obj) throws Exception {	
		obj.put("aaz411", "0");
		String ss = sys.callDBFuncProce("ic_az20.get_dqzkpc", "function", obj).toString();
		String result = ss.substring(9, 19) ;
		return  result;
	}
	/**
	 * 获取个人制卡批次
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String getMakeGRPc(HashMap obj) throws Exception {	
		obj.put("aaz411", "1");
		String ss = sys.callDBFuncProce("ic_az20.get_dqzkpc", "function", obj).toString();
		String result = ss.substring(9, 19) ;
		return  result;
	}
	/**
	 * 新增登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String insertUnit(HashMap obj) throws Exception {	 
		String ss = sys.callDBFuncProce("ic_az20.set_zkdj", "function", obj).toString();
		String[] str = ss.split("&");
		String result = str[0];
		return result;
	} 
	/**
	 * 制卡个人登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String selectUnit1(HashMap obj) throws Exception {	
		obj.put("jblx", "1");
		String ss = sys.callDBFuncProce("ic_az20.chk_zkdj", "function", obj).toString();
		return ss;
	} 
	/**
	 * 换卡个人登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String selectUnit2(HashMap obj) throws Exception {	
		obj.put("jblx", "H");
		String ss = sys.callDBFuncProce("ic_az20.chk_zkdj", "function", obj).toString();
		return ss;
	} 
	/**
	 * 制卡批量校验登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String checkdate(HashMap obj) throws Exception {	
		obj.put("jblx", "1");
		obj.put("czlx", "1");		
		String ss = sys.callDBFuncProce("ic_az20.set_zkdj_pl", "function", obj).toString();
		String[] str = ss.split("&");
		String result = str[1];
		return result;
	} 
	/**
	 * 制卡批量校验登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String checkhkdate(HashMap obj) throws Exception {	
		obj.put("jblx", "H");
		obj.put("czlx", "1");		
		String ss = sys.callDBFuncProce("ic_az20.set_zkdj_pl", "function", obj).toString();
		String[] str = ss.split("&");
		String result = str[1];
		return result;
	} 
	/**
	 * 确认制卡登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String insertUnitpl(HashMap obj) throws Exception{	
		obj.put("czlx", "2");
		obj.put("jblx", "1");
		String ss = sys.callDBFuncProce("ic_az20.set_zkdj_pl", "function", obj).toString();
		System.out.println(ss);
		String[] str = ss.split("&");
		String result = str[0];
		return result;
	} 
	/**
	 * 确认换卡登记信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public String insertUnitplhk(HashMap obj) throws Exception{	
		obj.put("czlx", "2");
		obj.put("jblx", "H");
		String ss = sys.callDBFuncProce("ic_az20.set_zkdj_pl", "function", obj).toString();
		String[] str = ss.split("&");
		String result = str[0];
		return result;
	} 
	/**
	 * 数据导出,下载
	 * @author duy
	 * @param obj
	 * @param parms
	 * @param response 
	 * @throws Exception
	 */
	public File exkcdate(String id) throws Exception {
		String sql = "select c1,c2,c3,c4,c5,EXECMSG from zj_tmp where execcode='-1' and EVENTID ='" + id + "'";
		File f = new File(Const.server_path + "/export_zkgl/zkgl_error" + id + ".xls");
		//如果父目录不在就创建
		if(!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}	
		if((!f.exists())) {
			f.createNewFile();
		}		
		sys.writeExcelTitle(new FileOutputStream(f), sql, new String[] { "社会保障卡号", "证件号", "姓名", "所属一级机", "所属二级机","错误信息"});																		 
		return f;
	}
}